<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 68KB
 *
 * An open source knowledge base script
 *
 * @package		68kb
 * @author		68kb Dev Team
 * @copyright	Copyright (c) 2009, 68 Designs, LLC
 * @license		http://68kb.com/user_guide/license.html
 * @link		http://68kb.com
 * @since		Version 1.0
 */

// ------------------------------------------------------------------------

/**
 * KB Controller
 *
 * Main Setup Controller
 *
 * @package		68kb
 * @subpackage	Controllers
 * @category	Controllers
 * @author		68kb Dev Team
 * @link		http://68kb.com/
 * @version 	$Id: kb.php 170 2009-12-11 05:25:01Z suzkaw68 $
 */
class Kb extends Controller
{
	/**
	* Constructor
	*
	* @return 	void
	*/
	function __construct()
	{
		parent::__construct();
		$this->load->library('core_migrate');
		$this->core_migrate->setverbose(TRUE);
		define('KB_VERSION', '##REVISION##');
	}
	
	// ------------------------------------------------------------------------
	
	/**
	* Index Controller
	*
	* Show the setup page
	*
	* @access	public
	*/
	function index()
	{
		global $error;
		$data['error']=$error;
		if($this->db->table_exists('articles'))
		{
			redirect('setup/kb/upgrade');
		}
		$data['nextStep'] = 2;
		$data['cache'] = $this->_writeable(KBPATH.'cache');
		$data['uploads'] = $this->_writeable(KBPATH.'uploads');
		$data['body'] = $this->load->view('setup/index', $data, true);
		$this->load->view('setup/layout.php', $data);
	}
	
	// ------------------------------------------------------------------------
	
	/**
	* Install step 1
	*
	* @access	public
	*/
	function install()
	{
		global $error;
		
		$data['error']=$error;
		$data['nextStep'] = 2;
		//$data['cache'] = $this->_writeable(KBPATH.'cache');
		$data['uploads'] = $this->_writeable(KBPATH.'uploads');
		$data['body'] = $this->load->view('setup/install-step1', $data, true);
		$this->load->view('setup/layout.php', $data);
	}
	
	// ------------------------------------------------------------------------
	
	/**
	* Run the install
	*
	* @access	public
	*/
	function run()
	{
		$username = $this->input->post('username', TRUE);
		$password = $this->input->post('password', TRUE);
		$email = $this->input->post('adminemail', TRUE);
		define('KB_EMAIL', $email);
		
		$data['log'] = $this->core_migrate->install();
		
		$insert_data = array(
			    'firstname' => 'admin',
			    'lastname' => 'acount',
			    'email' => $email,
			    'username' => $username,
			    'password' => md5($password),
			    'joindate' => time(),
				'level'	=> 1
			);
		$this->db->insert('users', $insert_data);
		
		$data['body'] = $this->load->view('setup/install-step2', $data, true);
		$this->load->view('setup/layout.php', $data);
	}
	
	// ------------------------------------------------------------------------
	
	/**
	* Do the upgrade
	*
	* @access	public
	*/
	function upgrade()
	{
		$data['log'] = $this->core_migrate->version();
		
		// now maintenance
		$version_data['value'] = KB_VERSION;
		$this->db->where('short_name', 'version');
		$this->db->update('settings', $version_data);

		//optimize db
		$this->load->dbutil();
		$this->dbutil->optimize_database();

		//delete cache
		$this->load->helper('file');
		delete_files($this->config->item('cache_path'));

		$data['body'] = $this->load->view('setup/upgrade', $data, true);
		$this->load->view('setup/layout.php', $data);
	}
	
	// ------------------------------------------------------------------------
	
	/**
	* Check if a file is writable.
	*
	* @param	string The file name.
	* @access	private
	*/
	private function _writeable($filename) 
	{
		global $error;
		if ( ! is_writable($filename)) 
		{
			$error=TRUE;
			return "<span style='color: red;'>ERROR!</span> Please CHMOD to 777";
		}
		else
		{
			return 'Ok';
		}
	}
	
	// ------------------------------------------------------------------------
	
	//
	// Migrate to a particular version.
	//
	function version($id = NULL)
	{
		if (is_null($id)) 
		{
			show_error("Must pass in an id.");
		}
		
		$migrate = $this->core_migrate->version($id);
		
		if ( ! $migrate)
		{
			show_error($this->core_migrate->error);
		}
		else
		{
			echo $migrate ." <br />Migration Successful<br />";
		}
	}
}

/* End of file kb.php */
/* Location: ./upload/includes/application/controllers/setup/kb.php */ 